/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("Product",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Entity.DomainModels;
using VOL.System.IServices;
using SqlSugar;
using BusFin.Read.Entity.DomainModels;
using VOL.Entity.DomainModels.ApiEntity.OutPut;
using StackExchange.Redis;
using Microsoft.AspNetCore.Authorization;

namespace VOL.System.Controllers
{
    public partial class ProductController
    {
        private readonly IProductService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly ISqlSugarClient _db;

        [ActivatorUtilitiesConstructor]
        public ProductController(
            IProductService service,
            IHttpContextAccessor httpContextAccessor,
            ISqlSugarClient db
        )
        : base(service)
        {
            _service = service;
            _db = db;
            _httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// 获取库存警告
        /// </summary>
        /// <param name="name"></param>
        /// <param name="wareHouseId"></param>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpGet,AllowAnonymous,Route("getstockwaring")]
        public async Task<IActionResult> GetStockWaring(string name,string wareHouseId,int index=1,int size=10)
        {
            try
            {

                int total = 0;

                var query = _db.Queryable<Product>()
                    .LeftJoin<wareHouse>((p, ware) => p.DefaultRepository == ware.wareHouseId)
                   
                    .Select((p, ware) => new StockWaring
                    {
                        DefaultRepository = ware.wareHouseName,
                       
                        // 这里需要根据 StockWaring 类的实际结构来选择适当的字段
                    },true);
                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where(p => p.ProductName.StartsWith(name));
                }
                if (wareHouseId!= "请选择")
                {
                    query = query.Where(p => p.DefaultRepository.Equals(wareHouseId));
                }

                total= query.Count();
                var pagedList = await query.ToPageListAsync(index, size);

                // 返回分页数据和总数
                var result = new
                {
                    TotalCount = total,
                    Items = pagedList
                };

                return Ok(result);

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
